﻿using Core.Tools.Cache;
using Core.Tools.IOC;
using Hardware.Reactor.Base;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UI.Application.Reactor.Models;
using UI.Application.Share.Config;
using UI.Application.Share.Reactor;

namespace UI.Application.Reactor.Services
{
    /// <summary>
    /// 切换反庆釜的管理器
    /// </summary>
    [ExposedService(Lifttime.Singleton,typeof(IReactorManager))]
    public class ReactorManager : ReactiveObject, IReactorManager
    {
        /// <summary>
        /// 当前选中的反应釜
        /// </summary>
        [Reactive]
        public IReactorModel Current { get; set ; }
        public IReactorModel Reactor1 { get ; set ; }
        public IReactorModel Reactor2 { get ; set; }

        /// <summary>
        /// 当前反应釜的观察者
        /// </summary>
        public IObservable<IReactorModel> Observable { get; set; }
        [Reactive]
        public ITemp1Reader Temp1Reader { get; set; }
        [Reactive]
        public ITemp2Reader Temp2Reader { get; set; }
        [Reactive]
        public ITempController TempController { get; set; }

        public void SelectReactor(object id)
        {
            Current = id.ToString() == "1" ? Reactor1 : Reactor2;
        }

        public ReactorManager(ICacheManager cacheManager,
            ITemp1Reader temp1Reader,
            ITemp2Reader temp2Reader,
            IPidProvider pidProvider,
            IReactorCommandFactory reactorCommandFactory,
            ITempController tempController
            )
        {
            Observable = this.WhenAnyValue(p => p.Current);


            Temp1Reader = temp1Reader;
            Temp2Reader = temp2Reader;
            TempController = tempController;

            Reactor1 = new ReactorModel(
                ReactorName.Reactor1, 
                temp1Reader,
                cacheManager,
                pidProvider,
                reactorCommandFactory, 
                tempController
                );

            Reactor2 = new ReactorModel(
                ReactorName.Reactor2, 
                temp2Reader,
                cacheManager, 
                pidProvider,
                reactorCommandFactory, 
                tempController
                );

            Current = Reactor1;//默认为1号反应釜
        }
    }
}
